<template>
  <div class="login">
    <div class="loginbox">
      <div class="loginLeft"></div>
      <div class="loginRight">
        <h3 class="title">登录</h3>
        <el-form :model="loginForm" ref="loginForm" :rules="rules" class="login-form">
          <el-form-item label="账号" prop="username" style="margin-top: 46px; margin-bottom: 0px">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
            </el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="margin-top: 46px; margin-bottom: 0px">
            <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
              @keyup.enter.native="handleLogin">
            </el-input>
          </el-form-item>
          <el-form-item style="width: 100%; margin-top: 60px; margin-bottom: 0px">
            <el-button :loading="loading" size="medium" type="primary" style="width: 100%"
              @click.native.prevent="handleLogin">
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script>
import { getCodeImg, BaseInfo } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import MD5 from "md5";
import { login } from "@/api/login";
export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
        ],
        password: [{ required: true, trigger: "blur", message: "请输入密码" }],
      },
      loading: false,
      // 验证码开关
      captchaOnOff: true,
      // 注册开关
      register: false,
      redirect: undefined,
      originUrl: "",
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true,
    },
  },
  created() {
    //删除所有
    localStorage.clear();
    this.getUrlNow();
    // localStorage.clear()
  },
  methods: {
    getUrlNow() {
      this.originUrl = window.location.origin + "/";
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          const username = this.loginForm.username.trim();
          const password1 = this.loginForm.password.trim();
          const password = MD5(`${MD5(password1)}oneninesix`);
          login(username, password)
            .then((res) => {
              if (!res || res.code != "200") {
                this.$message.error(res.info);
                this.loading = false;
                return;
              } else {
                let { token, ip, pk } = res.data;
                // 当前用户拉取user_info信息
                localStorage.setItem("token", token);
                localStorage.setItem("publicKey", pk);
                localStorage.setItem("Ioooop", ip);
                const userData = JSON.parse(res.data.userData);
                const params = { userId: userData.userID };
                // // 请求头带入token 再进行其他请求
                BaseInfo(params)
                  .then((res) => {
                    let userInfo = res.data || null;
                    if (userInfo) {
                      // this.$message.success(res.msg)
                      localStorage.setItem(
                        "ms_username",
                        this.loginForm.username
                      );
                      let str = JSON.stringify(userInfo);
                      localStorage.setItem("ms_userInfo", str);
                      this.$message.success("登录成功");
                      this.$router.push({ path: this.redirect || "/" });
                    } else {
                      this.$message.error("登录失败，接口返回数据异常！");
                    }
                  })
                  .catch((err) => {
                    this.loading = false;
                    console.log(err);
                  });
              }
            })
            .catch((err) => {
              this.loading = false;
              console.log(err);
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
@import "~@/assets/styles/mixin.scss";
@import "~@/assets/styles/variables.scss";

.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-size: cover;
  background: #01072f;
}

.loginbox {
  width: 76.25%;
  height: 75%;
  // background: pink;
}

.loginLeft {
  float: left;
  width: 50%;
  height: 100%;
  background-image: url("../assets/images/loginLeft.png");
  background-size: 100% 100%;
  // border: ;
}

.loginRight {
  float: right;
  width: 50%;
  height: 100%;
  position: relative;
  background-image: url("../assets/images/loginRight.png");
  background-size: 100% 100%;
}

.title {
  position: absolute;
  top: 17.5%;
  left: 17%;
  font-size: 36px;
  color: $base-menu-light-background;
}

.login-form {
  position: absolute;
  top: 30%;
  left: 17%;
  border-radius: 6px;
  background: #ffffff;
  width: 374px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}

.el-button {
  height: 7.5%;
  border-radius: 374px;
  background-color: $base-menu-light-background;
  font-size: 20px;
}
</style>

<style scoped>
/deep/ .el-form-item__label {
  font-size: 18px;
  color: #8089bb !important;
  margin-bottom: 3.3%;
}

/deep/ .el-input__inner {
  border: none;
  border-bottom: 1px solid #d8d7d7;
  padding-left: 0px;
}
</style>

<style scoped>
/deep/ .el-form-item__label {
  font-size: 18px;
  color: #8089bb !important;
  margin-bottom: 3.3%;
}

/deep/ .el-input__inner {
  border: none;
  border-bottom: 1px solid #d8d7d7;
  padding-left: 0px;
}
</style>
